package com.sinozo.jackson.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * BigDecimal格式化
 *
 * @author zhehen.lu
 * @date 2025/11/2 02:30
 */
public class BigDecimalSerializer extends JsonSerializer<BigDecimal> {
    @Override
    public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        if (value != null) {
            // 格式化为 2 位小数（如 321.00）
            gen.writeString(value.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        } else {
            gen.writeNull();
        }
    }
}
